//定义一个公共的组件盒子
import React, {
    Component
} from 'react'
import {
    hashHistory
} from 'react-router'
import {
    urlKey,
    getClassList,
    HttpService,
    jxtLocalStorage,
    Tips,
    addStatistics,
    pageNameList,
    funcCheck,
    taskFinish
} from '../../Tools'

import {
    appfn
} from '../../Js/public.js'
import WxServer from '../../Tools/WxServer';
export default class Roots extends Component {
    constructor(props) {
        super(props);
        this.state = {
            useno: false
        }
        this.useName = ''
    }
    componentWillMount() {
        const wherefrom = urlKey('wherefrom');
        if (wherefrom != '') {
            jxtLocalStorage();
            const router = location.hash.split('?')[0].toLocaleLowerCase()
            if (/integral/.test(router)) //统计积分入口
                addStatistics('/jf/goodsArchives/clickFirstPage')
            if (/info|integral|tasktoday|myorder|mypackage/.test(router)) //需要排除登录的页面
                return
            if (wherefrom == 'weixin') {
                sessionStorage.weixinmsg = urlKey('weixinmsg')
                this.wxToJXT();
            } else {
                appfn();
                const classid = urlKey('classid'),
                    classname = urlKey('classname');
                this.setClassId(classid == '', classid, classname);
            }
            _hmt.push(['_trackEvent', 'page', 'wherefrom', wherefrom]);
        } else {
            // if (!sessionStorage.ticket)
            //     this.Prompt('请在微信公众号随寻科技登录')
        }
    }
    componentDidMount() {
        //微信配置
        if (urlKey('wherefrom') == 'weixin') {
            if (/micromessenger/.test(navigator.userAgent.toLowerCase())) {
                if (sessionStorage.wxinit != 1)
                    WxServer.init()
            }
        }
        document.getElementById('loadwx').style.display = 'none'
    }

    //页面监听
    // componentWillReceiveProps(nextProps) {
    //     console.log(nextProps)
    // }

    //设置班级ID
    setClassId(status, classid, classname) {
        if (!sessionStorage.ticket)
            return
        if (status) {
            let pageName = location.hash.slice(2).split('?')[0].split('/')[0].toLocaleLowerCase()
            if (pageName == 'notice' || pageName == 'ntcdetail' || pageName == 'phonedirectories') //不需要获取班级的页面
                return
            this.useName = pageNameList[pageName]
            getClassList((classList) => {
                if (classList.length == 1) {
                    sessionStorage.classid = classList[0].id;
                    sessionStorage.classname = classList[0].name
                }
                if (classList.length == 0) {
                    Tips('当前帐号非任课教师<br/>部分功能无法使用', 0)
                    if (pageName == 'index')
                        funcCheck()
                    this.setState({
                        useno: true
                    })
                }
            })
        } else {
            sessionStorage.classid = classid;
            sessionStorage.classname = classname;
        }
    }

    //提示登录问题
    Prompt(msg) {
        if (document.querySelector('.maskin')) {
            document.querySelector('.maskin').style.display = 'block'
            Tips(msg, 0);
        } else {
            setTimeout(() => {
                this.Prompt(msg)
            }, 100)
        }
    }

    //微信去往家校通
    wxToJXT() {
        const type = sessionStorage.appType
        let data = {
            method: 'ajaxJybJSON_999PassLogin',
            passid: sessionStorage.phone,
            token: sessionStorage.appToken,
            type,
            studentid: sessionStorage.userid
        };
        if (type != 1) { //教师端
            document.title = '随寻通教师端'
            delete data['studentid']
        }

        HttpService.query({
            url: '/jxt-commonbackground/ajaxLoginSystem.do',
            data: data,
            async: false,
            success: (res => {
                if (res.code == 0) {
                    sessionStorage.ticket = res.r.ticket;
                    if (res.r.detail) {
                        sessionStorage.orgid = res.r.detail.orgId;
                        sessionStorage.orgname = res.r.detail.orgName;
                        sessionStorage.username = res.r.detail.username;
                        sessionStorage.userid = res.r.detail.userid; //用户userid
                        this.setClassId(type != 1, res.r.detail.depts[0].orgid, res.r.detail.depts[0].orgname);
                    }
                } else {
                    this.Prompt(res.msg);
                }
            }),
            fail: (msg) => {
                this.Prompt('连接服务器失败');
            }
        });
    }

    render() {
        const {
            useno
        } = this.state
        return (
            <div className='app'>
                {useno==true?<header className="header">
                        <a className="h_back" href='/jiaxiaotong/appback'></a>
                        <div className="h_title">{this.useName}</div>
                    </header>:this.props.children}
                <div className='maskin' style={{
                    display: 'none'
                }}></div>
            </div>
        );
    }
}
